<!---
	Copyright 2010 Edward Trudeau, Jeremy Battle
    This software is freely available under the MIT License: http://www.opensource.org/licenses/mit-license.php
    http://ayc.googlecode.com
    
--->

<cfcomponent displayname="FieldGateway" output="false" extends="_FieldGateway">
	
    <cffunction name="init" access="public" output="false" returntype="FieldGateway">
		<cfargument name="dsn" type="string" required="true" />
        
		<cfset super.init(arguments.dsn) />
        
		<cfreturn this />
	</cffunction>
	
	<cffunction name="getFieldsByPage" access="public" output="false" returntype="array">
		<cfargument name="PageId" type="Numeric" required="false" />
        <cfargument name="FieldID" type="numeric" required="false" />
		<cfargument name="ObjectName" type="string" required="false" />
		<cfargument name="FieldName" type="string" required="false" />
		<cfargument name="FieldFullName" type="string" required="false" />
		<cfargument name="FieldType" type="string" required="false" />
		<cfargument name="FieldClass" type="string" required="false" />
		<cfargument name="DataType" type="string" required="false" />
		<cfargument name="LabelText" type="string" required="false" />
		<cfargument name="LabelClass" type="string" required="false" />
		<cfargument name="PreFieldHTML" type="string" required="false" />
		<cfargument name="PostFieldHTML" type="string" required="false" />
		<cfargument name="InnerHTML" type="string" required="false" />
		<cfargument name="DataMaxLength" type="numeric" required="false" />
		<cfargument name="MinLength" type="numeric" required="false" />
		<cfargument name="DefaultsTo" type="string" required="false" />
		<cfargument name="ContentProvider" type="string" required="false" />
		<cfargument name="Required" type="boolean" required="false" />
		
		<cfset var qList = "" />	
        <cfset var arrObjects = arrayNew(1) />
		<cfset var tmpObj = "" />
		<cfset var i = 0 />	
        
		<cfquery name="qList" datasource="#variables.dsn#">
        	SELECT
				AFC.FieldID,
				ObjectName,
				FieldName,
				FieldFullName,
				FieldType,
				AFC.FieldClass,
				DataType,
				LabelText,
				LabelClass,
				PreFieldHTML,
				PostFieldHTML,
				InnerHTML,
				DataMaxLength,
				MinLength,
				DefaultsTo,
				ContentProvider,
				Required,
                FieldSeq,
                FieldLeft,
                FieldTop,
                APF.FieldClass AS PageFieldClass,
                FieldStyle
			FROM	aycFieldCatalog AFC, aycPageFields APF
			WHERE	AFC.FieldId = APF.FieldId
            AND PageId = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.PageId#" />
            <cfif structKeyExists(arguments,"FieldID") and len(arguments.FieldID)>
                AND	FieldID = <cfqueryparam value="#arguments.FieldID#" CFSQLType="cf_sql_integer" />
            </cfif>
            <cfif structKeyExists(arguments,"ObjectName") and len(arguments.ObjectName)>
                AND	ObjectName = <cfqueryparam value="#arguments.ObjectName#" CFSQLType="cf_sql_varchar" />
            </cfif>
            <cfif structKeyExists(arguments,"FieldName") and len(arguments.FieldName)>
                AND	FieldName = <cfqueryparam value="#arguments.FieldName#" CFSQLType="cf_sql_varchar" />
            </cfif>
            <cfif structKeyExists(arguments,"FieldFullName") and len(arguments.FieldFullName)>
                AND	FieldFullName = <cfqueryparam value="#arguments.FieldFullName#" CFSQLType="cf_sql_varchar" />
            </cfif>
            <cfif structKeyExists(arguments,"FieldType") and len(arguments.FieldType)>
                AND	FieldType = <cfqueryparam value="#arguments.FieldType#" CFSQLType="cf_sql_varchar" />
            </cfif>
            <cfif structKeyExists(arguments,"FieldClass") and len(arguments.FieldClass)>
                AND	FieldClass = <cfqueryparam value="#arguments.FieldClass#" CFSQLType="cf_sql_varchar" />
            </cfif>
            <cfif structKeyExists(arguments,"DataType") and len(arguments.DataType)>
                AND	DataType = <cfqueryparam value="#arguments.DataType#" CFSQLType="cf_sql_varchar" />
            </cfif>
            <cfif structKeyExists(arguments,"LabelText") and len(arguments.LabelText)>
                AND	LabelText = <cfqueryparam value="#arguments.LabelText#" CFSQLType="cf_sql_varchar" />
            </cfif>
            <cfif structKeyExists(arguments,"LabelClass") and len(arguments.LabelClass)>
                AND	LabelClass = <cfqueryparam value="#arguments.LabelClass#" CFSQLType="cf_sql_varchar" />
            </cfif>
            <cfif structKeyExists(arguments,"PreFieldHTML") and len(arguments.PreFieldHTML)>
                AND	PreFieldHTML = <cfqueryparam value="#arguments.PreFieldHTML#" CFSQLType="cf_sql_varchar" />
            </cfif>
            <cfif structKeyExists(arguments,"PostFieldHTML") and len(arguments.PostFieldHTML)>
                AND	PostFieldHTML = <cfqueryparam value="#arguments.PostFieldHTML#" CFSQLType="cf_sql_varchar" />
            </cfif>
            <cfif structKeyExists(arguments,"InnerHTML") and len(arguments.InnerHTML)>
                AND	InnerHTML = <cfqueryparam value="#arguments.InnerHTML#" CFSQLType="cf_sql_varchar" />
            </cfif>
            <cfif structKeyExists(arguments,"DataMaxLength") and len(arguments.DataMaxLength)>
                AND	DataMaxLength = <cfqueryparam value="#arguments.DataMaxLength#" CFSQLType="cf_sql_integer" />
            </cfif>
            <cfif structKeyExists(arguments,"MinLength") and len(arguments.MinLength)>
                AND	MinLength = <cfqueryparam value="#arguments.MinLength#" CFSQLType="cf_sql_integer" />
            </cfif>
            <cfif structKeyExists(arguments,"DefaultsTo") and len(arguments.DefaultsTo)>
                AND	DefaultsTo = <cfqueryparam value="#arguments.DefaultsTo#" CFSQLType="cf_sql_varchar" />
            </cfif>
            <cfif structKeyExists(arguments,"ContentProvider") and len(arguments.ContentProvider)>
                AND	ContentProvider = <cfqueryparam value="#arguments.ContentProvider#" CFSQLType="cf_sql_varchar" />
            </cfif>
            <cfif structKeyExists(arguments,"Required") and len(arguments.Required)>
                AND	Required = <cfqueryparam value="#arguments.Required#" CFSQLType="cf_sql_bit" />
            </cfif>
            ORDER BY FieldSeq
		</cfquery>
        
		<cfloop from="1" to="#qList.recordCount#" index="i">
			<cfset tmpObj = createObject("component","Field").init(argumentCollection=queryRowToStruct(qList,i)) />
			<cfset arrayAppend(arrObjects,tmpObj) />
		</cfloop>
				
		<cfreturn arrObjects />
	</cffunction>
    
    <cffunction name="getPageIdFromTitle" access="public" output="false" returntype="numeric">
    	<cfargument name="PageTitle" type="string" required="yes" />
        
        <cfset var qList = "" />
        
        <cfquery name="qList" datasource="#variables.dsn#">
            SELECT PageId FROM aycPages
            WHERE PageTitle = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.PageTitle#" />
        </cfquery>
        
    	<cfif qList.recordCount>
            <cfreturn qList.PageId />
        <cfelse>
        	<cfreturn 0 />
        </cfif>
    </cffunction>

</cfcomponent>


